/*
 Xern Entity Relocation Network
 Copyright (C) 2011 Ronie Salgado <roniesalg@gmail.com>

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/

#ifndef XERNP_SESSION_HPP
#define XERNP_SESSION_HPP

#include <set>
#include <deque>
#include <map>
#include <boost/shared_ptr.hpp>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include "Xern/DataTypes.hpp"

namespace XernProtocol
{
	using namespace Xern;
	namespace posix_time = boost::posix_time;
	using boost::asio::ip::udp;

	class Packet;
	struct IMessageDecoder;
	struct IMessage;
	class Session
	{
	public:
		enum SessionState
		{
			SS_DISCONNECTED = 0,
			SS_CONNECTING,
			SS_CONNECTED,
		};

		Session();
		~Session();

		u32 GetSessionID() const;
		u32 GetRemoteSessionID() const;

		void SetMessageDecoder(boost::shared_ptr<IMessageDecoder> decoder);
		boost::shared_ptr<IMessageDecoder> GetMessageDecoder() const;

		void Send(boost::shared_ptr<IMessage> message);
		boost::shared_ptr<IMessage> Receive();

	private:
		friend class GridNode;

		typedef std::map<u32, boost::shared_ptr<Packet> > PacketsWaitingAck;
		typedef std::set<u32> PacketsToAck;
		typedef std::map<u32, posix_time::ptime> ReceivedPackets;
		typedef std::deque<boost::shared_ptr<IMessage> > MessageQueue;

		/// Reliable packets that are waiting an acknowledge.
		PacketsWaitingAck packetsWaitingAck;

		/// Reliable packets that we have to acknowledge.
		PacketsToAck packetsToAck;

		/// Received packets. To avoid packet duplication.
		ReceivedPackets receivedPackets;

		/// Queued messages to send.
		MessageQueue sendQueue;

		/// Received messages.
		MessageQueue receiveQueue;

		/// Message decoder.
		boost::shared_ptr<IMessageDecoder> messageDecoder;

		/// Local session id.
		u32 sessionId;

		/// Remote session id.
		u32 remoteSessionId;

		/// Remote host endpoint.
		udp::endpoint remoteEndpoint;

		/// Last send time.
		posix_time::ptime lastSendTime;

		/// Last receive time.
		posix_time::ptime lastReceiveTime;

		// Session state.
		SessionState state;

		// Threading.
		boost::mutex sessionMutex;
	};

	// Struct to help identify a remote session.
	struct RemoteSessionId
	{
		RemoteSessionId(const udp::endpoint &ep, u32 id)
			: endpoint(ep), sessionId(id) {	}

		udp::endpoint endpoint;
		u32 sessionId;

		friend bool operator<(const RemoteSessionId &a, const RemoteSessionId &b)
		{
			if(a.sessionId != b.sessionId)
				return a.sessionId < b.sessionId;
			return a.endpoint < b.endpoint;
		}
	};

}; // namespace XernProtocol

#endif //XERNP_SESSION_HPP
